package com.crb.crm.manage.web.biz.msg;

import com.crb.crm.manage.entity.biz.TemplateSearchDTO;
import com.crb.crm.manage.entity.biz.TemplateVO;
import com.crb.crm.manage.pojo.User;
import com.crb.crm.manage.pojo.biz.Template;
import com.crb.crm.manage.service.biz.TemplateService;
import com.crb.crm.manage.utils.PageDataResult;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

/**
 * @author huangjian
 */
@Controller
@RequestMapping("/template")
public class TemplateController {
	
	private static final Logger logger = LoggerFactory
			.getLogger(TemplateController.class);
	@Autowired
	private TemplateService templateService;
	@RequestMapping("/templateList")
	public String toTemplateList() {
		return "biz/msg/templateList";
	}
	
	/**
	 * 分页查询模板列表
	 * @return ok/fail
	 */
	@RequestMapping(value = "/getTemplates", method = RequestMethod.POST)
	@ResponseBody
	@RequiresPermissions(value="getTemplates")
	public PageDataResult getTemplates(@RequestParam("page") Integer page,
                                   @RequestParam("limit") Integer limit, TemplateSearchDTO templateSearch) {
		logger.debug("分页查询模板列表！搜索条件：templateSearch：" + templateSearch + ",page:" + page
				+ ",每页记录数量limit:" + limit);
		PageDataResult pdr = new PageDataResult();
		try {
			if (null == page) {
				page = 1;
			}
			if (null == limit) {
				limit = 10;
			}
			// 获取模板
			pdr = templateService.getTemplates(templateSearch, page, limit);
			logger.debug("模板列表查询=pdr:" + pdr);

		} catch (Exception e) {
			e.printStackTrace();
			logger.error("模板列表查询异常！", e);
		}
		return pdr;
	}

	
	/**
	 * 设置模板[新增或更新]
	 * @return ok/fail
	 */
	@RequestMapping(value = "/setTemplate", method = RequestMethod.POST)
	@ResponseBody
	@RequiresPermissions({"templateAdd","templateEdit"})
	public String setTemplate(Template template) {
		logger.debug("设置模板[新增或更新]！template:" + template);
		try {
			if (null == template) {
				logger.debug("设置模板[新增或更新]，结果=请您填写模板信息");
				return "请您填写模板信息";
			}
			User existUser = (User) SecurityUtils.getSubject().getPrincipal();
			if (null == existUser) {
				logger.debug("设置模板[新增或更新]，结果=您未登录或登录超时，请您登录后再试");
				return "您未登录或登录超时，请您登录后再试";
			}
			//user.setInsertUid(existUser.getId());
			// 设置模板[新增或更新]
			logger.info("设置模板[新增或更新]成功！template=" + template + "，操作的用户ID=" + existUser.getId());
			return templateService.setTemplate(template);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("设置模板[新增或更新]异常！", e);
			return "操作异常，请您稍后再试";
		}
	}

	/**
	 * 删除模板
	 * @return ok/fail
	 */
	@RequestMapping(value = "/delTemplate", method = RequestMethod.POST)
	@ResponseBody
	@RequiresPermissions("templateDelete")
	public String delTemplate(@RequestParam("id") Integer id) {
		logger.debug("删除模板！id:" + id);
		String msg = "";
		try {
			if (null == id) {
				logger.debug("删除模板，结果=请求参数有误，请您稍后再试");
				return "请求参数有误，请您稍后再试";
			}
			User existUser = (User) SecurityUtils.getSubject().getPrincipal();
			if (null == existUser) {
				logger.debug("删除模板，结果=您未登录或登录超时，请您登录后再试");
				return "您未登录或登录超时，请您登录后再试";
			}
			// 删除模板
			msg = templateService.setDelTemplate(id, 1);
			logger.info("删除模板:" + msg + "。userId=" + id + "，操作用户id:"+ existUser.getId());
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("删除模板异常！", e);
			msg = "操作异常，请您稍后再试";
		}
		return msg;
	}

	/**
	 *
	 * @描述：恢复模板
	 * @创建人：wyait
	 * @创建时间：2018年4月27日 上午9:49:14
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/recoverTemplate", method = RequestMethod.POST)
	@ResponseBody
	@RequiresPermissions("templateDelete")
	public String recoverTemplate(@RequestParam("id") Integer id) {
		logger.debug("恢复模板！id:" + id);
		String msg = "";
		try {
			User existUser = (User) SecurityUtils.getSubject().getPrincipal();
			if (null == existUser) {
				return "您未登录或登录超时，请您登录后再试";
			}
			if (null == id ) {
				return "请求参数有误，请您稍后再试";
			}
			// 删除模板
			msg = templateService.setDelTemplate(id, 0);
			logger.info("恢复模板【" + this.getClass().getName() + ".recoverTemplate】"
					+ msg + "。模板Id=" + id + "，操作的用户ID=" + existUser.getId());
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("恢复模板【" + this.getClass().getName()
					+ ".recoverUser】模板异常！", e);
			msg = "操作异常，请您稍后再试";
		}
		return msg;
	}

	/**
	 * 查询模板数据
	 * @return map
	 */
	@RequestMapping(value = "/getTemplate", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> getTemplate(@RequestParam("id") Integer id) {
		logger.debug("查询模板数据！id:" + id);
		Map<String, Object> map = new HashMap<>();
		try {
			if (null == id) {
				logger.debug("查询模板数据==请求参数有误，请您稍后再试");
				map.put("msg", "请求参数有误，请您稍后再试");
				return map;
			}
			// 查询模板
			TemplateVO templatevo = templateService.getTemplate(id);
			logger.debug("查询模板数据！templatevo=" + templatevo);
			if (null != templatevo) {
				map.put("template", templatevo);
				map.put("msg", "ok");
			} else {
				map.put("msg", "查询模板信息有误，请您稍后再试");
			}
			logger.debug("查询模板数据成功！map=" + map);
			return map;
		} catch (Exception e) {
			e.printStackTrace();
			map.put("msg", "查询模板错误，请您稍后再试");
			logger.error("查询模板数据异常！", e);
		}
		return map;
	}


}
